//
//  FotoView.m
//  iDent
//
//  Created by Marius Corega on 6/10/09.
//  Copyright 2009 Marius Corega. All rights reserved.
//

#import "FotoView.h"



@implementation FotoView

@synthesize foto;
@synthesize index;
@synthesize fotos;

-(id)init
{
	self = [super init];
	
	foto = [[Foto alloc] init];
	
	return self;
}

- (id)initWithFrame:(NSRect)frameRect
{
	self = [super initWithFrame:frameRect];
	
	foto = [[Foto alloc] init];
	isHovered = isSelected = NO;
	index = -1;
	
//	[self addTrackingRect:frameRect owner: self userData:nil assumeInside:NO ];
	
	return self;
}

-(void)loadFotos
{
	for(int i=0; i<[fotos count]; i++)
	{
		
		NSInteger thumb_height = 50;
		NSInteger thumb_width = 80;
		

		FotoView *imgView = [[FotoView alloc] initWithFrame:NSMakeRect(i*thumb_width, 0, thumb_width , thumb_height) ];
		imgView.foto = [fotos objectAtIndex:i];

		imgView.index = i;
				
		[self  addSubview:imgView ];
 		
	}
	//[self setNeedsDisplay:YES];
}

- (void) drawRect: (NSRect)rect
{
	NSPoint pp;
	pp.x = pp.y = 0;
	
	if (isSelected || isHovered)
	{
		[[NSColor redColor] set]; 
		NSRectFill(rect);
	}
	
	
		[foto.thumb drawAtPoint:pp fromRect:NSMakeRect(0, 0, 0, 0) operation:2 fraction:1.0];


 }

-(void) setImage:(NSImage*)img
{
	foto.image = img;
}

-(void) setThumb:(NSImage*)img
{
	foto.thumb = img;
}

-(void) showThumb
{
	if(foto != nil) [self setNeedsDisplay:YES];	
}

- (void)mouseDown:(NSEvent *)theEvent
{
	 	isSelected =YES;
	[self setNeedsDisplay:YES];
}

- (void)mouseUp:(NSEvent *)theEvent 
{
	isSelected = NO;
	[self setNeedsDisplay:YES];
	Controller *del = (Controller*)[[NSApplication sharedApplication] delegate];
	[del arataFotocuID:self.index];
}

- (void)mouseEntered:(NSEvent *)theEvent
{
	
	isHovered = YES;
	[self setNeedsDisplay:YES];
}

- (void)mouseExited:(NSEvent *)theEvent
{
	isHovered = NO;
	[self setNeedsDisplay:YES];
}

-(void)dealloc
{
	[foto release];
	[fotos removeAllObjects];
	[fotos release];
	
	[super dealloc];
}

@end
